//
//  HKDatePickerViewController.m
//  Pods
//
//  Created by 胡锦涛 on 2021/1/24.
//

#import "HKAddressPickerViewController.h"
#import <HKLibFlexBox/YGLayout.h>
#import <HKLibFlexBox/UIView+Yoga.h>
#import <HKLibCommon/HKPrefixHeader.h>
#import <HKLibBase/HKLibBaseHeader.h>

/// 架构框架
#import <Bifrost/BifrostHeader.h>
#import <HKMediator/UIKitModuleService.h>
#import <HKLibAddressPicker/HKAddressPickView.h>


@interface HKAddressPickerViewController ()
///  地址选择按钮
@property (nonatomic, strong) QMUIButton * addressPickerButton ;
/// 当前选择的省份
@property (nonatomic, copy) NSString *currentProvince;
/// 当前选择的市
@property (nonatomic, copy) NSString *currentCity;
/// 当前选择的区县
@property (nonatomic, copy) NSString *currentArea;
/// 当前选择的区县编码
@property (nonatomic, copy) NSString *currentCityId;
/// 配送区域城市ID
@property (nonatomic, strong) NSString * deliverAreaCityId ;
/// 背景滚动视图
@property (nonatomic, strong) UIScrollView *bgScrollView ;

@end

@implementation HKAddressPickerViewController
+ (void)load
{
    [Bifrost bindURL:kRouteAddressPickerPage
           toHandler:^id _Nullable(NSDictionary * _Nullable parameters) {
        return [HKAddressPickerViewController new];
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorWhite;
    self.title = @"地址选择";
    [self setupUI];
}

- (void)setupUI
{
    [self.view configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
        layout.isEnabled = YES;
        layout.alignItems = YGAlignStretch;
    }];
    self.bgScrollView = [[UIScrollView alloc] init] ;
    self.bgScrollView.backgroundColor = [UIColor grayColor];
    [self.bgScrollView configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled = YES;
        layout.flexDirection = YGFlexDirectionColumn;
        layout.width = YGPointValue(SCREEN_WIDTH);
        layout.height = YGPointValue(SCREEN_HEIGHT);
    }];
    [self.view addSubview:self.bgScrollView];
    // 配送区域选择器
    [self createAddressPickerView];
    [self.bgScrollView.yoga applyLayoutPreservingOrigin:YES];
    self.bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view.yoga applyLayoutPreservingOrigin:YES];
    
}

/// 配送区域选择器
- (void)createAddressPickerView
{
    UIView * flexContainer = [[UIView alloc] init];
    [flexContainer configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled     = YES;
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.paddingHorizontal = YGPointValue(5);
        layout.marginTop = YGPointValue(50);
        layout.marginBottom = YGPointValue(20);
    }];
    YGLayoutConfigurationBlock layoutBlock = ^(YGLayout * layout){
        layout.isEnabled     = YES;
        layout.height= YGPointValue(60);
        layout.marginHorizontal = YGPointValue(5);
        layout.flexGrow = 1;
    };
    
    QMUIButton * dayPickerButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [dayPickerButton hk_setNormalTitle:@"请选择配送区域"];
    [dayPickerButton hk_setNormalTitleColor:UIColorBlack];
    dayPickerButton.backgroundColor = UIColorRed;
    [dayPickerButton configureLayoutWithBlock:layoutBlock];
    [dayPickerButton hk_addTarget:self action:@selector(switchRouteDeliverArea)];
    [flexContainer addSubview:dayPickerButton];
    _addressPickerButton = dayPickerButton;
    
    [self.bgScrollView addSubview:flexContainer];
}
#pragma mark -- ButtonAction --

/// 配送区域
- (void)switchRouteDeliverArea
{
    HKAddressPickView *selectV = [[HKAddressPickView alloc] initWithLastContent:self.currentProvince ? @[self.currentProvince, self.currentCity, self.currentArea] : nil];
    HKWeakSelf
    selectV.confirmSelect = ^(NSArray *address) {
        HKStrongSelf
        NSString * currentProvince = address[0];
        NSString * currentCity = address[1];
        NSString * currentArea = address[2];
        NSString * currentCityId = address[3];
        self.currentProvince = currentProvince;
        self.currentCity = currentCity;
        self.currentArea = currentArea;
        self.deliverAreaCityId = currentCityId;
        [self.addressPickerButton hk_setNormalTitle:[NSString stringWithFormat:@"%@-%@-%@", currentProvince, currentCity, currentArea]];
    };
    [selectV show];
}
@end
